package com.iplant.base.po;

import java.io.File;
import java.util.*;

import com.iplant.base.utils.general.LockHelper;
import org.apache.commons.io.FileUtils;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;

import com.alibaba.fastjson.JSON;
import com.iplant.base.utils.general.StringUtils;

public class BPMResource<T> {

    private static final Logger logger = LoggerFactory.getLogger(BPMResource.class);

    private LockHelper mLockHelper;
    private String FilePath = "";

    private LinkedList<T> Value = new LinkedList<T>();

    public BPMResource() {
        super();
        mLockHelper = LockHelper.getLock(BPMResource.class);
    }

    public BPMResource(Class<T> clazz, String wSourceName) {
        super();
        mLockHelper = LockHelper.getLock(clazz, wSourceName);
    }


    public BPMResource(String wPath, Class<T> clazz) {
        super();
        mLockHelper = LockHelper.getLock(clazz, wPath);
        if (StringUtils.isEmpty(wPath))
            return;
        FilePath = wPath;
        try {
            File wFile = new File(FilePath);
            if (wFile.exists()) {
                String wJson = FileUtils.readFileToString(wFile, "UTF-8");
                Value = new LinkedList<>(JSON.parseArray(wJson, clazz));
            }
        } catch (Exception e) {
            logger.error(e.toString());
        }
    }

    public int size() {
        if (Value == null)
            return 0;
        return Value.size();
    }

    public void add(T wValue) {
        if (wValue == null)
            return;
        synchronized (mLockHelper) {
            Value.add(wValue);
        }
    }

    public void addAll(Collection<T> wValue) {
        if (wValue == null)
            return;
        synchronized (mLockHelper) {
            Value.addAll(wValue);
        }
    }

    public T get() {
        T wResult;
        synchronized (mLockHelper) {
            if (Value.size() == 0)
                return null;
            wResult = Value.poll();
        }
        return wResult;
    }

    public List<T> getAll() {
        synchronized (mLockHelper) {
            List<T> wResult = new ArrayList<T>(Value);
            Value.clear();
            return wResult;
        }
    }

    public List<T> getAll(int wSize) {
        synchronized (mLockHelper) {
            List<T> wResult = new ArrayList<>();
            for (int i = 0; i < wSize; i++) {
                if (Value.size() == 0)
                    break;
                wResult.add(Value.poll());
            }
            return wResult;
        }
    }

    /*
     * 获取后不删除
     *
     */
    public List<T> getNotAll() {
        List<T> wResult = new ArrayList<T>(Value);
        return wResult;
    }

    public void Save() {
        try {
            if (StringUtils.isEmpty(FilePath))
                return;
            synchronized (mLockHelper) {
                File wFile = new File(FilePath);
                if (!wFile.exists())
                    wFile.createNewFile();
                FileUtils.write(wFile, StringUtils.toJSONString(Value), "UTF-8");
                Value.clear();
            }

        } catch (Exception e) {
            logger.error(e.toString());
        }
    }

    public void Save(String wPath) {
        try {
            if (StringUtils.isEmpty(wPath))
                return;
            synchronized (mLockHelper) {
                File wFile = new File(wPath);
                if (!wFile.exists())
                    wFile.createNewFile();
                FileUtils.write(wFile, StringUtils.toJSONString(Value), "UTF-8");
                Value.clear();
            }
        } catch (Exception e) {
            logger.error(e.toString());
        }
    }

}
